import React from 'react';
import { Button, Input, Icon } from 'antd';
import { StateType } from '@/common-typings';
import { types, getFormatDate, isEmptyParam, toTime } from '@/utils/utils';
import { currency } from '@/utils/tools';

/**
 * 格式化数据
 * @param text
 * @param item
 */
export const formatText = (text: any, item: StateType) => {
  if (isEmptyParam(text)) {
    return '--';
  }
  if (text === '|') {
    return '';
  }
  // 格式化时间戳
  if (item.formatTimeStamp) {
    return getFormatDate(text, true);
  }
  // 格式化时间
  if (item.toTime) {
    return toTime(text);
  }
  // 格式化金额
  if (item.formatAmount) {
    return currency(text);
  }
  // 格式化人员
  if (item.formatMember) {
    return currency(text, '', 0);
  }
  return text;
};

/**
 * 获取字符串的真实长度，中文2个字符，英文数据1个字符
 * @param str
 */
export const getStrLen = (str: any) => {
  let realLength = 0;
  let charCode;
  for (let i = 0; i < str.toString().length; i += 1) {
    charCode = str.toString().charCodeAt(i);
    if (charCode >= 0 && charCode <= 128) realLength += 1;
    else realLength += 2;
  }
  return realLength;
};

export const getFiltersDataByConfig = (filters: StateType, columns: any[]) => {
  const result = {};

  Object.keys(filters).forEach(key => {
    if (types(filters[key]) === 'array' && filters[key].length) {
      // console.log(123123, filters, columns);
    }
  });

  return result;
};

export const getColumnSearchProps = (
  dataIndex: string,
  self: any,
  placeholder: string = '搜索',
) => ({
  filterDropdown: ({ setSelectedKeys, selectedKeys, confirm, clearFilters }: any) => (
    <div style={{ padding: 8 }}>
      <Input
        ref={node => {
          self.searchInput = node;
        }}
        placeholder={placeholder}
        value={selectedKeys[0]}
        onChange={e => setSelectedKeys(e.target.value ? [e.target.value] : [])}
        onPressEnter={() => confirm()}
        style={{ width: 188, margin: '0 auto', marginBottom: 8, display: 'block' }}
      />
      <Button
        type="primary"
        onClick={async () => {
          confirm();
        }}
        icon="search"
        size="small"
        style={{ width: 90, margin: 0 }}
      >
        搜索
      </Button>
      <Button
        onClick={() => {
          clearFilters();
          confirm();
        }}
        size="small"
        style={{ width: 90, marginLeft: 8 }}
      >
        重置
      </Button>
    </div>
  ),
  filterIcon: (filtered: any) => (
    <Icon type="search" style={{ color: filtered ? '#1890ff' : undefined }} />
  ),
});
